﻿using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using System.IO;
using System.Threading.Tasks;
using System;
using System.Net;
using Android.Graphics;
using AndroidX.AppCompat.App;
using System.Collections.Generic;

namespace BicolorTest
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        string[] RedBallArr = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33" };
        string[] BlueBallArr = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16" };
        int ClickCount = 0;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            Button ClickButton = FindViewById<Button>(Resource.Id.button1);
            EditText ShowEditText1 = FindViewById<EditText>(Resource.Id.editText1);
            EditText ShowEditText2 = FindViewById<EditText>(Resource.Id.editText2);
            EditText ShowEditText3 = FindViewById<EditText>(Resource.Id.editText3);
            EditText ShowEditText4 = FindViewById<EditText>(Resource.Id.editText4);
            EditText ShowEditText5 = FindViewById<EditText>(Resource.Id.editText5);
            ClickButton.Click += delegate
            {
                List<CBicolor> CBicolorList = new List<CBicolor>();
                CBicolor CBicolorItem = new CBicolor();
                int I = 0;
                ClickCount = ClickCount + 1;
                while (CBicolorList.Count < 5)
                {
                    CBicolorItem = GetCBicolor();
                    CBicolor TempCBicolorItem = CBicolorList.Find(v => v.BicolorStr == CBicolorItem.BicolorStr);
                    if (TempCBicolorItem != null)
                    {
                        TempCBicolorItem.AppearCount = TempCBicolorItem.AppearCount + 1;
                    }
                    else
                    {
                        CBicolorList.Add(CBicolorItem);
                    }
                }
                for (I = 0; I < CBicolorList.Count; I++)
                {
                    CBicolorItem = CBicolorList[I];

                    if (CBicolorItem != null)
                    {
                        switch (I)
                        {
                            case 0:
                                ShowEditText1.Text = "红球：" + CBicolorItem.RedBallStr + "；蓝球：" + CBicolorItem.BlueBallStr + "；次数：" + CBicolorItem.AppearCount.ToString();
                                break;
                            case 1:
                                ShowEditText2.Text = "红球：" + CBicolorItem.RedBallStr + "；蓝球：" + CBicolorItem.BlueBallStr + "；次数：" + CBicolorItem.AppearCount.ToString();
                                break;
                            case 2:
                                ShowEditText3.Text = "红球：" + CBicolorItem.RedBallStr + "；蓝球：" + CBicolorItem.BlueBallStr + "；次数：" + CBicolorItem.AppearCount.ToString();
                                break;
                            case 3:
                                ShowEditText4.Text = "红球：" + CBicolorItem.RedBallStr + "；蓝球：" + CBicolorItem.BlueBallStr + "；次数：" + CBicolorItem.AppearCount.ToString();
                                break;
                            case 4:
                                ShowEditText5.Text = "红球：" + CBicolorItem.RedBallStr + "；蓝球：" + CBicolorItem.BlueBallStr + "；次数：" + CBicolorItem.AppearCount.ToString();
                                break;
                            default:
                                break;
                        }
                    }
                }
                ClickButton.Text = "按钮,点击次数：" + ClickCount.ToString();
            };
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        public CBicolor GetCBicolor()
        {
            CBicolor CBicolorItem = new CBicolor();
            Int32 RedBallArrLength = RedBallArr.Length;
            Int32 BlueBallArrLength = BlueBallArr.Length;

            List<string> RedBallStrList = new List<string>();
            byte[] buffer = Guid.NewGuid().ToByteArray();//将Guid转换为字节数组
            int seed = BitConverter.ToInt32(buffer, 0);//利用BitConvert方法把字节数组转换为整数

            Random RedRandomNum = new Random(seed);
            while (RedBallStrList.Count < 6)
            {
                string TempRedStr = RedBallArr[RedRandomNum.Next(RedBallArrLength)];
                if (!RedBallStrList.Contains(TempRedStr))
                {
                    RedBallStrList.Add(TempRedStr);
                }
            }

            string TempRedBallStr = "";
            for (Int32 J = 0; J < RedBallStrList.Count; J++)
            {
                if (TempRedBallStr == "")
                {
                    TempRedBallStr = RedBallStrList[J];
                }
                else
                {
                    TempRedBallStr = TempRedBallStr + "," + RedBallStrList[J];
                }
            }
            RedBallStrList.Clear();

            Random BlueRandomNum = new Random(seed);
            string TempBlueBallStr = "";
            TempBlueBallStr = BlueBallArr[BlueRandomNum.Next(BlueBallArrLength)];

            string TempBicolorStr = "";
            TempBicolorStr = TempRedBallStr + "," + TempBlueBallStr;


            CBicolorItem.AppearCount = 1;
            CBicolorItem.RedBallStr = TempRedBallStr;
            CBicolorItem.BlueBallStr = TempBlueBallStr;
            CBicolorItem.BicolorStr = TempBicolorStr;
            return CBicolorItem;
        }
    }
}